﻿using ConfExpert.Datasets;
using DevTeam;


using System.Collections.Generic;
using System.Data;

namespace ConfExpert.Forms.ImportWizards.Barkod
{
    public partial class CafeCotton2 : XImportWizard
    {
        public CafeCotton2()
        {
            InitializeComponent();

            this.Init("Cafe Coton barkodu yükleme", true);
            this.FileDone += DestexTip1_FileDone;
            //this.SplitterWidthPercentage = 50;
            this.ProcessStart += Destex_LF_Tip2_ProcessStart;
        }

        string DNs;
        public CafeCotton2(string DNs) :
            this()
        {
            this.DNs = DNs;
        }

        void Destex_LF_Tip2_ProcessStart()
        {
            //dtArt = new DataTable();
            //dtArt.Columns.Add("Barkod", typeof(string));
            //dtArt.Columns.Add("Model", typeof(string));
            //dtArt.Columns.Add("Art", typeof(string));
            //dtArt.Columns.Add("Taglia", typeof(string));

            //dsResult = new DataSet();
            //dsResult.Tables.Add(dtArt);

            btnSave.ItemClick -= BtnSave_ItemClick;
            btnSave.ItemClick += BtnSave_ItemClick;
        }

        private void BtnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (splitter.PanelVisibility == DevExpress.XtraEditors.SplitPanelVisibility.Panel1 && MasterGrid.DataTable().Rows.Count > 0)
                Go(MasterGrid.Serialize());
        }

        //DataTable dtArt;
        //DataSet dsResult;

        //string xml = string.Empty;
        void DestexTip1_FileDone(DataSet ds, string FileName)
        {
            DataTable dt = ds.Tables[0];
            DataRowCollection rows = dt.Rows;
            DataColumnCollection cols = dt.Columns;

            Dictionary<string, int> indexes = new Dictionary<string, int>();

            DataRow dr0 = dt.Rows[0];
            for (int colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
            {
                switch (dr0[colIndex].ToString())
                {
                    case "CODES BARRES EAN 13":
                    case "CODES BARRES EAN13":
                    case "Barkod":
                        indexes.Add("Barkod", colIndex); break;
                    case "CATEGORIE":
                    case "Model":
                        indexes.Add("Model", colIndex); break;
                    case "MODELES":
                    case "Art":
                        indexes.Add("Art", colIndex); break;
                    case "TAILLES":
                    case "Taglia":
                        indexes.Add("Taglia", colIndex); break;
                    //case "Model2": indexes.Add("Model2", colIndex); break;
                    //case "Art2": indexes.Add("Art2", colIndex); break;
                }
            }

            G = new MyData.CCBarkodDataTable();
            for (int i = 1; i < dt.Rows.Count; i++)
            {
                DataRow dr = rows[i];

                ConfExpert.Datasets.MyData.CCBarkodRow drG = G.NewCCBarkodRow();

                drG.Barkod = dr[indexes["Barkod"]].ToStr();
                drG.Model = dr[indexes["Model"]].ToStr();
                drG.Art = dr[indexes["Art"]].ToStr();
                drG.Taglia = dr[indexes["Taglia"]].ToStr();

                G.AddCCBarkodRow(drG);
            }

            //G.Merge(dtArt);
            xml = G.Serialize();

            Go(string.Empty);
        }
        MyData.CCBarkodDataTable G;
        string xml;

        private void Go(string esleme)
        {
            DataSet d = ExecuteDataSet("UpdCcBarkodDosyasi2", 339, DNs, xml, esleme);
            splitter.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Panel1;

            if (d.Tables[0].Rows.Count > 0)
            {
                MasterGrid.Init(d.Tables[0]);
                MasterGrid.FormatAsPercentage("Score");
                MasterGrid.SetViewCaption("Farklı yazımlar");
                MasterGrid.AddSummary();
                MasterGrid.BestFitColumns();
                ShowErrorMessage("Sistemdeki bazı model/artikel çiftleri, dosyadakilerle eşleşmedi. Lütfen listeyi kontrol edin !");
            }
            else
            {
                ShowMessage("Barkod bilgileri başarı ile aktarıldı.");
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        private void CafeCotton2_Load(object sender, System.EventArgs e)
        {

        }

    }
}